package com.ksxy.afingerprint.ui.activity

import android.annotation.TargetApi
import android.os.Build
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import com.ksxy.afingerprint.R
import com.ksxy.afingerprint.core.KeyStoreHelper
import com.ksxy.afingerprint.ui.activity.base.AbstractReturnActionActivity
import com.ksxy.afingerprint.utils.showShortToast
import kotlinx.android.synthetic.main.activity_create_key.*

class CreateKeyActivity : AbstractReturnActionActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initView()
    }

    private fun initView() {
        btn_generate.setOnClickListener(this)
    }

    override fun getLayoutResId(): Int {
        return R.layout.activity_create_key
    }

    override fun onClick(v: View?) {
        if(v!!.id == R.id.btn_generate){
            createKey()
        }
    }

    @TargetApi(Build.VERSION_CODES.M)
    private fun createKey() {
        val alias = edt_key_alias.text.toString().trim()
        if(TextUtils.isEmpty(alias)){
            edt_key_alias_wrapper.error = getString(R.string.no_null)
            return
        }
        if (KeyStoreHelper.isExistedKey(alias)){
            edt_key_alias_wrapper.error = getString(R.string.existed_key)
            return
        }
        edt_key_alias_wrapper.error = ""
        if(KeyStoreHelper.createSecretKey(alias)){
            showShortToast(getString(R.string.create_key_success))
        }else{
            showShortToast(getString(R.string.create_key_fail))
        }
    }
}
